package com.example.checkin.vn;

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.example.checkin.search.FragmentSearch;
import com.example.checkin.vn.adapter.Item;
import com.example.checkin.vn.adapter.NavDrawerListAdapter;
import com.example.checkin.vn.home.FragmentHome;
import com.example.checkin.vn.hotel.FragmentHotel;
import com.example.checkin.vn.util.Contants;
import com.example.checkin.vn.util.IResult;
import com.example.checkin.vn.util.Utils;

public class MainActivity extends SherlockFragmentActivity {
	private ArrayList<Item> listMenuLeft;
	private ListView listMenu;
	public static ResultCallBack callbackClick;
	private ActionBarDrawerToggle mDrawerToggle;
	private DrawerLayout mDrawerLayout;
	private int idActive;
	private LinearLayout leftMenu;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		callbackClick = new ResultCallBack();
		setContentView(R.layout.activity_main);
		listMenu = (ListView) findViewById(R.id.rbm_listview);
		View header = getLayoutInflater().inflate(R.layout.head_contact, null);
		listMenu.addHeaderView(header);
		addMenuLeft(R.menu.rightmenu);
		leftMenu = (LinearLayout) findViewById(R.id.lineMenu);

		mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
		mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
				GravityCompat.START);
		getSupportActionBar().setDisplayHomeAsUpEnabled(false);
		getSupportActionBar().setHomeButtonEnabled(true);

		listMenu.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				if (arg2 == 0) {
					return;
				}
				idActive = listMenuLeft.get(arg2 - 1).getRegId();
				mDrawerLayout.closeDrawers();
			}
		});

		mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
				R.drawable.ic_navigation_drawer,
				R.drawable.ic_navigation_drawer,
				R.drawable.ic_navigation_drawer) {

			@Override
			public void onDrawerStateChanged(int arg0) {

			}

			@Override
			public void onDrawerOpened(View arg0) {
				super.onDrawerOpened(arg0);
			}

			@Override
			public void onDrawerClosed(View arg0) {
				super.onDrawerClosed(arg0);
				addFragment(idActive);
			}

		};

		mDrawerLayout.setDrawerListener(mDrawerToggle);
		addFragment(Contants.HOME);
	}

	public void addFragment(int key) {
		FragmentManager fragmentManager = getSupportFragmentManager();
		FragmentTransaction ft = fragmentManager.beginTransaction();
		Fragment fragment = null;

		switch (key) {
		case R.id.right_khachsan:

			fragment = (Fragment) fragmentManager
					.findFragmentByTag(Contants.TAG_HOTEL);

			if (fragment == null) {
				fragment = FragmentHotel.getInstance();
				ft.addToBackStack(null);
				ft.replace(R.id.frame_container, fragment, Contants.TAG_HOTEL);
			}

			break;

		case Contants.SEARCH:

			fragment = (Fragment) fragmentManager
					.findFragmentByTag(Contants.TAG_SEARCH);
			if (fragment == null) {
				fragment = FragmentSearch.getInstance();
				ft.addToBackStack(null);
				ft.replace(R.id.frame_container, fragment, Contants.TAG_SEARCH);
			}

			break;
		case Contants.HOME:

			fragment = (Fragment) fragmentManager
					.findFragmentByTag(Contants.TAG_HOME);
			if (fragment == null) {
				fragment = FragmentHome.getInstance();
				ft.replace(R.id.frame_container, fragment, Contants.TAG_HOME);
			}

			break;

		}
		if (fragment != null) {
			ft.show(fragment);
		}
		ft.commit();
	}

	public class ResultCallBack implements IResult {

		@Override
		public void getResult(int arg1, String arg2) {
			addFragment(arg1);
		}

	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		boolean drawerOpenLeft = mDrawerLayout.isDrawerOpen(leftMenu);
		int id = item.getItemId();
		switch (id) {
		case android.R.id.home:
			if (drawerOpenLeft) {
				mDrawerLayout.closeDrawer(leftMenu);
			} else {
				mDrawerLayout.openDrawer(leftMenu);
			}
			break;

		case Contants.SEARCH:
			addFragment(id);
			break;
		}

		return super.onOptionsItemSelected(item);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		boolean isLight = SampleList.THEME == R.style.Theme_Sherlock_Light;
		menu.add(1, Contants.SEARCH, Menu.NONE, "Tìm kiếm")
				.setIcon(
						isLight ? R.drawable.ic_timkiem : R.drawable.ic_timkiem)
				.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

		menu.add(0, Contants.OPTION_MENU, Menu.NONE, " Lựa chọn ")
				.setIcon(isLight ? R.drawable.ic_option : R.drawable.ic_option)
				.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

		return true;
	}

	public void addMenuLeft(int menu) {
		listMenuLeft = Utils.getMenu(MainActivity.this, menu);
		NavDrawerListAdapter adapter = new NavDrawerListAdapter(
				MainActivity.this, R.layout.drawer_list_item, listMenuLeft);

		listMenu.setAdapter(adapter);
	}

	@Override
	public void onBackPressed() {

		Fragment myAbout = getSupportFragmentManager().findFragmentByTag(
				Contants.TAG_HOTEL);
		if (myAbout != null && myAbout.isVisible()) {
			FragmentManager fragmentManager = getSupportFragmentManager();
			fragmentManager.popBackStack();
			return;
		}

		Fragment mySearch = getSupportFragmentManager().findFragmentByTag(
				Contants.TAG_SEARCH);
		if (mySearch != null && mySearch.isVisible()) {
			FragmentManager fragmentManager = getSupportFragmentManager();
			fragmentManager.popBackStack();
			return;
		}
	}
}
